package managers;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import files.Files;

public class EncodingManager {

	public static final String DEFAULT_ENCODING = "ISO-8859-1";
	
	private FileConnection fc;
	private InputStream fis;

	private String encoding = null;

	public EncodingManager() throws IOException {
		String encodingFile = Files.getBookFolder() + "/Encoding.txt";
		fc = (FileConnection) Connector.open(encodingFile);

		if (fc.exists()) {
			readEncodingFromFile();
		} else {
			encoding = DEFAULT_ENCODING;
		}
	}

	private void readEncodingFromFile() throws IOException {
		byte[] b = new byte[20];
		fis = fc.openInputStream();
		int length = fis.read(b, 0, 20);
		fis.close();
		encoding = new String(b, 0, length);
	}

	public String getEncoding() {
		return encoding;
	}
}
